package orgamenu.ihm.table;

/* 
* This example is from javareference.com 
* for more information visit, 
* http://www.javareference.com 
*/ 

//package

//import statements
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*; 
import javax.swing.table.*;
import java.util.List;
import java.util.*;

/**
 * This class creates a JTable with rows of variable height 
 * @author Rahul Sapkal(rahul@javareference.com)
 */
public class VariableRowHeightTableDemo extends JApplet
       implements ActionListener
{ 
    //static variables
         
    //public variables
    private String m_images[] = {"puppy.jpg",
                               "ash.jpg",
                               "jr.gif",
                               "cliff.jpg",
                               "bridge.jpg"}; 
    
    private JButton m_launch;
     
    public void init() 
    { 
        getContentPane().setLayout(new BorderLayout());
                
        m_launch = new JButton("Launch Variable Row Height Table Demo");
        m_launch.addActionListener(this);
                    
        getContentPane().add(BorderLayout.CENTER, m_launch);
    } 
    
    private List getAllImages()
    {
        List imageList = new ArrayList();
        
        MediaTracker mt = new MediaTracker(this);
    
        for(int i = 0; i < m_images.length; i++)
        {
            Image image = getImage(getCodeBase(), m_images[i]);
            imageList.add(new ImageIcon(image));        
            mt.addImage(image, i);
        }
  
        try 
        { 
            mt.waitForAll(); 
        } 
        catch (InterruptedException e) 
        { 
            e.printStackTrace(); 
        } 
        
        return imageList;
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource().equals(m_launch))
        {
            JFrame demoFrame = new JFrame("Variable Row Height Table Demo");
            
            ImageTableModel imageTableModel = new ImageTableModel(getAllImages());
            
            JTable imageTable= new JTable(imageTableModel);
            imageTable.getColumnModel().getColumn(0).setCellRenderer(new VariableRowHeightRenderer());
            
            demoFrame.getContentPane().add(new JScrollPane(imageTable));
            
            demoFrame.pack();            
            demoFrame.setVisible(true);
        }    
    }
    
    /**
     * This class is the table cell renderer
     * @author Rahul Sapkal(rahul@javareference.com)
     */
    protected class VariableRowHeightRenderer extends JLabel  implements TableCellRenderer
    {
        public VariableRowHeightRenderer()
        {
            super();
            setOpaque(true);
            setHorizontalAlignment(JLabel.CENTER);
        }

        public Component getTableCellRendererComponent(JTable table,
                     Object value, boolean isSelected, boolean hasFocus,
                     int row, int column)
        {
            if(isSelected)
            {
                setBackground(UIManager.getColor("Table.selectionBackground"));
            }
            else
            {
                setBackground(Color.BLACK);
            }

            if (hasFocus) 
            {
                setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
                
                if (table.isCellEditable(row, column)) 
                {
                    super.setForeground( UIManager.getColor("Table.focusCellForeground") );
                    super.setBackground( UIManager.getColor("Table.focusCellBackground") );
                }
            } 
            else 
            {
                setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
            }

            //create the icon from the image and set it on the label
            setIcon((ImageIcon)(value));
            
            //check if the preferred height of this component is not equal to
            //the row height, then set the preferred height of this component as
            //the row height, else dont do anything.
            if(getPreferredSize().height != table.getRowHeight(row))
            {
                table.setRowHeight(row, getPreferredSize().height);
            }
            
            return this;
        }
    }        
    
    /**
     * This class is the table model
     * @author Rahul Sapkal(rahul@javareference.com)
     */
    public class ImageTableModel extends AbstractTableModel 
    { 
        //Columns Number.
        public static final int    IMG_COL = 0;                                    
             
         
        //Names of the columns
        public String[] m_colNames = {"Variable Dimension Images"
                                     }; 
        // Types of the columns.
        public Class[]  m_colTypes = {Image.class 
                                     }; 
         
        //store the data
        List m_imageList; 
         
        /** 
        * Constructor 
        */ 
        public ImageTableModel(List imageList) 
        { 
            super(); 
         
            //store the data
            m_imageList = imageList; 
        }
         
        public int getColumnCount() 
        {   
            return m_colNames.length; 
        } 
        
        public int getRowCount()     
        {   
            return m_imageList.size(); 
        } 
        
        public String getColumnName(int col) 
        { 
            return m_colNames[col]; 
        }          
         
        public Object getValueAt(int row, int col) 
        { 
            switch(col) 
            { 
                case IMG_COL : return (ImageIcon)m_imageList.get(row); 
            } 

            return new String(); 
        } 
    } 
}  